package com.jintoufs.web.action.trade;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.baseinfo.Depth;
import com.jintoufs.domain.baseinfo.Site;
import com.jintoufs.domain.baseinfo.SiteCoin;
import com.jintoufs.domain.baseinfo.Trade;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.baseinfo.*;
import com.jintoufs.service.trade.TradeService;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Controller
@Scope("prototype")
@RequestMapping("/trade")
public class TradeAction extends SupportAction {
    @Resource
    private TradeService tradeService;
    @Resource
    private MarketService marketService;
    @Resource
    private CoinService coinService;
    @Resource
    private MarketDepthService marketDepthService;
    @Resource
    private TickerService tickerService;

    @RequestMapping("/list")
    public String list(Model model) throws IOException {
        Map map = new HashMap();
        map.put("status", Constants.TRADE_STATUS_NEW);
        List<Map<String, Object>> list = tradeService.queryByParams(null);
        model.addAttribute("listEntity", list);
        return "trade/list";
    }

    @RequestMapping("/cancel")
    @SystemControllerLog(description = "撤銷交易")
    @ResponseBody
    public AjaxMsg cancel(Trade trade) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            ajaxMsg.setCode("200");
            ajaxMsg.setMessage("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("操作失败");
        }
        return ajaxMsg;
    }

    @RequestMapping("/buy")
    @SystemControllerLog(description = "下单")
    @ResponseBody
    public AjaxMsg buy(Trade trade, String siteName) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            ajaxMsg.setCode("200");
            ajaxMsg.setMessage("更新成功");
            tradeService.trade(trade, siteName);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("操作失败");
        }
        return ajaxMsg;
    }

    @RequestMapping("/depth")
    @ResponseBody
    public AjaxMsg depth(Integer siteId, Integer coinId, String siteName, String coinName, String unitCode) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Depth depth = new Depth();
        Map siteAmap = new HashMap();
        siteAmap.put("siteId", siteId);
        siteAmap.put("coinId", coinId);
        List<Map<String, Object>> listA = tickerService.getLastestPrice(siteAmap);
        Double price1 = getPrice(unitCode, listA);
        try {
            ajaxMsg.setCode("200");
            ajaxMsg.setMessage("更新成功");
            Map<String, Object> site = new HashMap();
            site.put("id", siteId);

            Map<String, Object> paramsCoin = new HashMap();
            paramsCoin.put("siteId", siteId);
            paramsCoin.put("coinId", coinId);
            List<Map<String, Object>> coins = coinService.querySiteCoinByParams(paramsCoin);
            // marketService.coinw(site, coins);
            switch (siteName) {
                case "coinw":
                    String symbol = coins.get(0).get("price_api").toString().split("=")[2];
                    depth = marketDepthService.coinw(symbol);
                    break;
                case "liqui":
                    depth = marketDepthService.liqui(coinId, coinName, unitCode, price1);
                    break;
            }
            ajaxMsg.setDatas(depth);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("操作失败");
        }
        return ajaxMsg;
    }
}
